package com.taotao.admin.rest;

import com.rop.client.CompositeResponse;
import com.taotao.admin.MyRopClient;
import com.taotao.admin.pojo.TableUI;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.BaseDomain;
import com.taotao.rop.database.domain.City;
import com.taotao.rop.database.domain.Manager;
import com.taotao.rop.database.domain.Role;
import com.taotao.rop.response.BaseResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/manager")
public class ManagerController {
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Long pageSize = Long.parseLong(request.getParameter("pageSize"));
        Long pageNo = Long.parseLong(request.getParameter("pageNo"));

        Manager condition = new Manager();
        condition.setPageSize(pageSize);
        condition.setPageNo(pageNo);

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(condition, BaseResponse.class, "manager.list", "1.0");
        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                TableUI tableUI = new TableUI();
                List<LinkedHashMap<String, Object>> respData = (List<LinkedHashMap<String, Object>>) resp.getData();
                tableUI.setTotal(resp.getMsg());
                tableUI.setRows(respData);
                return tableUI;
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/role/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object findRole(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        Role condition = new Role();
        condition.setId(Long.parseLong(id));
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(condition, BaseResponse.class, "role.list", "1.0");

        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                return resp.getData();
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/city/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object findCity(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        City condition = new City();
        condition.setId(Long.parseLong(id));
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(condition, BaseResponse.class, "city.list", "1.0");

        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                return resp.getData();
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/allrole", method = RequestMethod.POST)
    @ResponseBody
    public Object findAllRoles(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Integer roleLevel = Integer.parseInt(request.getParameter("roleLevel"));
        Role condition = new Role();
        condition.setRoleLevel(roleLevel);

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(condition, BaseResponse.class, "role.list", "1.0");

        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                return resp.getData();
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/allcity", method = RequestMethod.POST)
    @ResponseBody
    public Object findAllCities(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(new BaseDomain(), BaseResponse.class, "city.list", "1.0");

        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                return resp.getData();
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Manager manager = null;
        try {
            manager = getManagerFromRequest(request);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (manager != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(manager, BaseResponse.class, "manager.insert", "1.0");
            if (response.isSuccessful()){
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse().getSubErrors().get(0);
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Manager manager = null;
        try {
            manager = getManagerFromRequest(request);
            manager.setId(Long.parseLong(id));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (manager != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(manager, BaseResponse.class, "manager.update", "1.0");
            if (response.isSuccessful()){
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse().getSubErrors().get(0);
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/updatePsd/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object updatePsd(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        String password = request.getParameter("password");
        Manager manager;
        try {
            manager = new Manager();
            manager.setId(Long.parseLong(id));
            manager.setPassword(password);
        } catch (Exception e) {
            manager = null;
            e.printStackTrace();
        }
        if (manager != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(manager, BaseResponse.class, "manager.update", "1.0");
            if (response.isSuccessful()){
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse().getSubErrors().get(0);
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Manager manager = new Manager();
        manager.setId(Long.parseLong(id));
        manager.setStatus(Constant.STATUS_DELETE);
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(manager, BaseResponse.class, "manager.update", "1.0");
        if (response.isSuccessful()){
            return response.getSuccessResponse();
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    private Manager getManagerFromRequest(HttpServletRequest request) throws ParseException {
        String worknum = request.getParameter("worknum");
        String name = request.getParameter("name");
        String roleId = request.getParameter("roleId");
        String appId = request.getParameter("appId");
//        String cityId = request.getParameter("cityId");
        String alias = request.getParameter("alias");
//        String gender = request.getParameter("gender");
//        String birthday = request.getParameter("birthday");
        String telephone = request.getParameter("telephone");
        String email = request.getParameter("email");
//        String address = request.getParameter("address");
        String status = request.getParameter("status");

        Manager manager = new Manager();
        manager.setWorknum(worknum);
        manager.setName(name);
        manager.setRoleId(Long.parseLong(roleId));
        manager.setAppId(appId);
//        if (cityId != null) {
//            manager.setCityId(Long.parseLong(cityId));
//        }
        manager.setAlias(alias);
//        manager.setGender(Integer.parseInt(gender));
//        if (birthday != null && birthday != "") {
//            manager.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
//        }
        manager.setTelephone(telephone);
        manager.setEmail(email);
//        manager.setAddress(address);
        manager.setStatus(Integer.parseInt(status));
        return manager;
    }
}
